/** 主题存储键 */
export const storageThemeKey = 'THEME_KEY';

/** 主题类型 */
export const themeTypes = ['blue', 'red', 'green', 'white'];

/** 默认主题 */
export const defaultThemeType = 'blue';

/** 主题数据 */
export const themeDatas = [{
	text: '天真蓝',
	value: 'blue',
	navigationBarTextStyle: '#FFFFFF',
	navigationBarBackgroundColor: '#007AFF',
	backgroundColor: '#F8F8F8',
	tabBarTextColor: '#7A7E83',
	tabBarSelectedColor: '#007AFF',
	tabBarBorderStyle: 'black',
	tabBarBackgroundColor: '#E2E2E2'
}, {
	text: '霓好红',
	value: 'red',
	navigationBarTextStyle: '#FFFFFF',
	navigationBarBackgroundColor: '#FF1A1A',
	backgroundColor: '#F8F8F8',
	tabBarTextColor: '#7A7E83',
	tabBarSelectedColor: '#FF1A1A',
	tabBarBorderStyle: 'black',
	tabBarBackgroundColor: '#E2E2E2'
}, {
	text: '蕉布绿',
	value: 'green',
	navigationBarTextStyle: '#FFFFFF',
	navigationBarBackgroundColor: '#0D800D',
	backgroundColor: '#F8F8F8',
	tabBarTextColor: '#7A7E83',
	tabBarSelectedColor: '#0D800D',
	tabBarBorderStyle: 'black',
	tabBarBackgroundColor: '#E2E2E2'
}, {
	text: '谷得白',
	value: 'white',
	navigationBarTextStyle: '#000000',
	navigationBarBackgroundColor: '#EFEBDC',
	backgroundColor: '#F8F8F8',
	tabBarTextColor: '#7A7E83',
	tabBarSelectedColor: '#0A0A0D',
	tabBarBorderStyle: 'black',
	tabBarBackgroundColor: '#E2E2E2'
}]

/**
 * 获取当前主题
 */
export function getCurrentTheme() {
	let currentThemeData = uni.getStorageSync(storageThemeKey);
	// console.log(currentSkinData);
	if (currentThemeData === undefined | currentThemeData === null | currentThemeData === '') {
		// 设置默认主题
		currentThemeData = setCurrentTheme(defaultThemeType);
	}
	if (currentThemeData) {
		setAppTheme(currentThemeData);
	}
	return currentThemeData;
};

/**
 * 设置当前主题
 * @param {String} themeType - 主题类型：blue | red | green | white
 */
export function setCurrentTheme(themeType) {
	// console.log('setCurrentSkin', themeType);
	if (!themeTypes.includes(themeType)) {
		uni.showToast({
			icon: 'error',
			title: '主题类型必须为：blue | red | green | white'
		});
		return;
	}
	const themeData = getSkinTheme(themeType);
	if (themeData !== undefined && themeData !== null) {
		// console.log('setCurrentSkin', themeData);
		uni.setStorageSync(storageSkinKey, themeData);
		setAppTheme(themeData);
	}
	return themeData;
};

/**
 * 根据主题类型获取主题数据
 * @param {String} themeType - 主题类型：blue | red | green | white
 */
export function getThemeData(themeType) {
	// console.log('setCurrentSkin', themeType);
	if (!themeTypes.includes(themeType)) {
		uni.showToast({
			icon: 'error',
			title: '主题类型必须为：blue | red | green | white'
		});
		return;
	}
	for (let i = 0; i < themeDatas.length; i++) {
		const themeData = themeDatas[i];
		if (themeData && themeData.value === themeType) {
			return themeData;
		}
	}
	return null;
};

/**
 * 根据主题数据设置App主题
 * @param {String} themeData - 主题数据
 */
export function setAppTheme(themeData) {
	// console.log('setAppSkin', themeData);
	if (themeData) {
		// 设置页面导航条颜色
		uni.setNavigationBarColor({
			frontColor: themeData.navigationBarTextStyle,
			backgroundColor: themeData.navigationBarBackgroundColor
		});
		// 设置窗口的背景色
		uni.setBackgroundColor({
		    backgroundColor: themeData.backgroundColor
		});
		// 设置 tabBar 样式
		uni.setTabBarStyle({
			color: themeData.tabBarTextColor,
			selectedColor: themeData.tabBarSelectedColor,
			borderStyle: themeData.tabBarBorderStyle,
			backgroundColor: themeData.tabBarBackgroundColor
		});
		// console.log('setAppSkin', themeData.navigationBarBackgroundColor);
	}
};
